package com.server.llh.llhserverwork.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;


import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.bean.Constants;
import com.server.llh.llhserverwork.bean.SerInitProPackage;
import com.server.llh.llhserverwork.port.OrderServerInterface;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 服务项显示
 */
public class LLHServerCateContentAdapter  extends BaseAdapter{
	List<SerInitProPackage> listData;

	OrderServerInterface serverInterface;
	public List<SerInitProPackage> getListData() {
		return listData;
	}
	public void setListData(List<SerInitProPackage> listData) {
		this.listData = listData;
	}
	public void registerCallBack(OrderServerInterface serverInterfaced) {
		serverInterface = serverInterfaced;
	}
	String[] titleArr;
	private Context context;
	public LLHServerCateContentAdapter(Context context,List<SerInitProPackage> serviceInitProPack) {
		this.context=context;
		this.listData=serviceInitProPack;
		this.titleArr = context.getResources().getStringArray(R.array.unit_name);

	}
	@Override
	public int getCount() {
		if (listData == null || listData.size() == 0) {
			return 0;
		}
		return listData.size();
	}

	@Override
	public Object getItem(int position) {
		return listData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.server_cate_item, parent, false);
			holder = new ViewHolder();
			holder.server_image = (ImageView) convertView
					.findViewById(R.id.server_image);
			holder.server_name = (TextView) convertView
					.findViewById(R.id.server_name);
			holder.server_org = (RatingBar) convertView
					.findViewById(R.id.server_org);
			holder.server_price = (TextView) convertView
					.findViewById(R.id.server_price);
			holder.server_miaos = (TextView) convertView
					.findViewById(R.id.server_miaos);
			holder.select = (TextView) convertView
					.findViewById(R.id.select);
			holder.select_shoping = (TextView) convertView
					.findViewById(R.id.select_shoping);
			holder.tv_num = (TextView) convertView
					.findViewById(R.id.tv_num);
			
			holder.im_reduce = (ImageView) convertView
					.findViewById(R.id.im_reduce);
			holder.food_add = (ImageView) convertView
					.findViewById(R.id.food_add);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final SerInitProPackage infoData = listData.get(position);
		holder.server_name.setText(infoData.getPackName());
//		holder.server_org.setText(infoData.getOrgName());
		if (infoData.getUnit()!=0){
			holder.server_price.setText("¥"+infoData.getPrice()+"元/"+titleArr[infoData.getUnit()-1].toString());
		}else {
			holder.server_price.setText("¥"+infoData.getPrice()+"元/"+titleArr[infoData.getUnit()].toString());
		}
		holder.server_miaos.setText(infoData.getReamark());
		holder.tv_num.setText(""+infoData.getBuyNum());
//		holder.select.setText("购买");
		holder.food_add.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				infoData.buyNum++;
				notifyDataSetChanged();
				if (serverInterface != null) {

					serverInterface.refreshShoppingList(infoData);
				}
			}
		});
		holder.im_reduce.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				infoData.buyNum--;
				if (infoData.buyNum <= 0) {
					infoData.buyNum = 0;
				}
				notifyDataSetChanged();
				if (serverInterface != null) {
					serverInterface.refreshShoppingList(infoData);
				}
			}
		});
		holder.select.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
			long packId=infoData.getPackId();
//			Intent intent =new Intent(context,ServerContentActivity.class);
//			intent.putExtra("packId", String.valueOf(packId));
//			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			context.startActivity(intent);
			}
		});
		holder.select_shoping.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
//				long packId=infoData.getPackId();
//				Intent intent =new Intent(context,ServerContentActivity.class);
//				intent.putExtra("packId", String.valueOf(packId));
//				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//				context.startActivity(intent);
			}
		});
		
		if (infoData.getPictureUrl()!=null) {

			Picasso.with(context).load(Constants.SER_IMAGE_URL+infoData.getPictureUrl()).into( holder.server_image);

		} else {
			holder.server_image.setImageResource(R.drawable.icon_launcher);
		}
		
		return convertView;
	}
	class ViewHolder{
		ImageView server_image;
		TextView server_name,server_price,server_miaos,select,select_shoping,tv_num;
		RatingBar server_org;
		ImageView im_reduce,food_add;
	}

}
